﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessAccountNowDeposit
// 文件功能描述：帐户现存款数据访问层
//
// 
// 创建标识：2012-06-12 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessAccountNowDeposit
    {
        /// <summary>
        /// 获取已帐户现存款
        /// </summary>
        /// <returns></returns>
        public DataSet GetList(int pagesize, int startindex, string depname, string datetime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY 账号 ASC) rownumber,* from(SELECT EMPNO 账号,EMPNO 工号,姓名,部门名称,CONVERT(CHAR(10),入职日期,120) 开户日期,MAXDATE,ISNULL(t2.BALANCE,0) 账户余额" +
                                ",'封户否' = " +
                                "     CASE " +
                                "         WHEN 卡状态 ='正常' THEN ''" +
                                "         WHEN 卡状态 ='销卡' THEN '封户'" +
                                "         WHEN 卡状态 ='挂失卡' THEN '封户'" +
                                "      END" +
                                " FROM FILE_PERSONNEL_VIEW  T1 " +
                                "LEFT JOIN (" +
                                "SELECT A.PID,A.RDateTime,A.BALANCE/100.00 BALANCE,MAXDATE FROM CON_RECORD A " +
                                "INNER JOIN " +
                                "( " +
                                "SELECT  MAX(RDATETIME) AS MAXDATE,PID " +
                                "FROM CON_RECORD where 1=1 " + datetime +
                                " GROUP BY PID  " +
                                ") B ON A.PID=B.PID AND A.RDateTime=B.MAXDATE) T2 " +
                                "ON T1.PID=T2.PID " +
                                "WHERE 卡状态<>'未发卡')z " +
                                "where 1=1 " + depname + ") B1 where B1.rownumber >" + startrow.ToString() + " and B1.rownumber <" + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取已帐户现存款
        /// </summary>
        /// <returns></returns>
        public DataSet GetListAll(string depname, string datetime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select ROW_NUMBER() OVER(ORDER BY 账号 ASC) rownumber,* from(SELECT EMPNO 账号,EMPNO 工号,姓名,部门名称,CONVERT(CHAR(10),入职日期,120) 开户日期,MAXDATE,ISNULL(t2.BALANCE,0) 账户余额" +
                                ",'封户否' = " +
                                "     CASE " +
                                "         WHEN 卡状态 ='正常' THEN ''" +
                                "         WHEN 卡状态 ='销卡' THEN '封户'" +
                                "         WHEN 卡状态 ='挂失卡' THEN '封户'" +
                                "      END" +
                                " FROM FILE_PERSONNEL_VIEW  T1 " +
                                "LEFT JOIN (" +
                                "SELECT A.PID,A.RDateTime,A.BALANCE/100.00 BALANCE,MAXDATE FROM CON_RECORD A " +
                                "INNER JOIN " +
                                "( " +
                                "SELECT  MAX(RDATETIME) AS MAXDATE,PID " +
                                "FROM CON_RECORD where 1=1 " + datetime +
                                " GROUP BY PID  " +
                                ") B ON A.PID=B.PID AND A.RDateTime=B.MAXDATE) T2 " +
                                "ON T1.PID=T2.PID " +
                                "WHERE 卡状态<>'未发卡')z " +
                                "where 1=1 " + depname;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 获取已帐户现存款条数
        /// </summary>
        /// <param name="strDateTime"></param>
        /// <returns></returns>
        public int GetCount(string depname, string datetime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select count(*) from(select ROW_NUMBER() OVER(ORDER BY 账号 ASC) rownumber,* from(SELECT EMPNO 账号,EMPNO 工号,姓名,部门名称,CONVERT(CHAR(10),入职日期,120) 开户日期,MAXDATE,ISNULL(t2.BALANCE,0) 账户余额" +
                                ",'封户否' = " +
                                "     CASE " +
                                "         WHEN 卡状态 ='正常' THEN ''" +
                                "         WHEN 卡状态 ='销卡' THEN '封户'" +
                                "         WHEN 卡状态 ='挂失卡' THEN '封户'" +
                                "      END" +
                                " FROM FILE_PERSONNEL_VIEW  T1 " +
                                "LEFT JOIN (" +
                                "SELECT A.PID,A.RDateTime,A.BALANCE/100.00 BALANCE,MAXDATE FROM CON_RECORD A " +
                                "INNER JOIN " +
                                "( " +
                                "SELECT  MAX(RDATETIME) AS MAXDATE,PID " +
                                "FROM CON_RECORD where 1=1 " + datetime +
                                " GROUP BY PID  " +
                                ") B ON A.PID=B.PID AND A.RDateTime=B.MAXDATE) T2 " +
                                "ON T1.PID=T2.PID " +
                                "WHERE 卡状态<>'未发卡')z " +
                                "where 1=1 " + depname + ") z1";
                object count = db.ExecuteScalar(conn, CommandType.Text, query);
                return Convert.ToInt32(count);
            }
        }

        /// <summary>
        /// 获取已帐户现存款合计
        /// </summary>
        /// <param name="strDateTime"></param>
        /// <returns></returns>
        public decimal GetAllMoney(string depname, string datetime)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select sum(账户余额) from(select ROW_NUMBER() OVER(ORDER BY 账号 ASC) rownumber,* from(SELECT EMPNO 账号,EMPNO 工号,姓名,部门名称,CONVERT(CHAR(10),入职日期,120) 开户日期,MAXDATE,ISNULL(t2.BALANCE,0) 账户余额" +
                                ",'封户否' = " +
                                "     CASE " +
                                "         WHEN 卡状态 ='正常' THEN ''" +
                                "         WHEN 卡状态 ='销卡' THEN '封户'" +
                                "         WHEN 卡状态 ='挂失卡' THEN '封户'" +
                                "      END" +
                                " FROM FILE_PERSONNEL_VIEW  T1 " +
                                "LEFT JOIN (" +
                                "SELECT A.PID,A.RDateTime,A.BALANCE/100.00 BALANCE,MAXDATE FROM CON_RECORD A " +
                                "INNER JOIN " +
                                "( " +
                                "SELECT  MAX(RDATETIME) AS MAXDATE,PID " +
                                "FROM CON_RECORD where 1=1 " + datetime +
                                " GROUP BY PID  " +
                                ") B ON A.PID=B.PID AND A.RDateTime=B.MAXDATE) T2 " +
                                "ON T1.PID=T2.PID " +
                                "WHERE 卡状态<>'未发卡')z " +
                                "where 1=1 " + depname + ") z1";
                object allmoney = db.ExecuteScalar(conn, CommandType.Text, query);
                return Convert.ToDecimal(allmoney);
            }
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public DataSet GetdeptName()
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select Dep_ID,deptName from DeptView order by Dep_ID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
    }
}
